#! /usr/bin/env python3
# -*- coding: utf-8 -*-


"""
B+树
"""


class BTreeNode:
    def __init__(self, n=4, leaf=False):
        self.keys = []  # 键
        self.values = []  # 值
        self.children = []  # 子节点
        self.leaf = leaf  # 是否为叶子节点
        self.n = n  # 阶数

    def __getitem__(self, key):
        for i, k in enumerate(self.keys):
            if key < k:
                if self.leaf:
                    return None
                else:
                    return self.children[i][key]
        return self.children[-1][key]

    def __setitem__(self, key, value):
        for i, k in enumerate(self.keys):
            if key == k:
                self.values[i] = value
                return
            elif key < k:
                self.keys.insert(i, key)
                self.values.insert(i, value)
                if self.leaf:
                    return
                else:
                    self.children.insert(i, BTreeNode(self.n))


if __name__ == '__main__':
    node1 = BTreeNode()
